// components/Login/Login.jsx
import React from "react";
import { Form, Input, Button, Typography, Checkbox } from "antd";
import { UserOutlined, LockOutlined } from "@ant-design/icons";
import { fetchUsersLogin } from "../request/hytApi";
// 引入 CSS 模块
import login from "../styles/login.module.css";
const { Title } = Typography;

// 登录跳转
const Login = () => {
  const onFinish = async (values) => {
    console.log("Received values: ", values);
    // username: 'super', password: '666'
    const res = await fetchUsersLogin(values);
    if (res.code === 1) {
      console.log("res", res);
      localStorage.setItem("userInfo", JSON.stringify(res));
      window.location.href = "/dashboard";
    }
  };

  return (
    <div className={login.container}>
      <div className={login.loginBox}>
        <Title className={login.loginTitle} level={3}>
          东软医院HIS信息管理平台
        </Title>
        <Form name="login" onFinish={onFinish} layout="vertical">
          {/* 用户名输入框 */}
          <Form.Item
            name="account"
            rules={[{ required: true, message: "请输入您的用户名/邮箱" }]}
          >
            <Input prefix={<UserOutlined />} placeholder="用户名" />
          </Form.Item>

          {/* 密码输入框 */}
          <Form.Item
            name="password"
            rules={[{ required: true, message: "请输入密码" }]}
          >
            <Input.Password prefix={<LockOutlined />} placeholder="密码" />
          </Form.Item>

          {/* 登录按钮 */}
          <Form.Item>
            <Button type="primary" block htmlType="submit">
              登录
            </Button>
          </Form.Item>

          {/* 同意协议复选框 */}
          <Form.Item
            name="agreement"
            valuePropName="checked"
            rules={[{ required: true, message: "请勾选同意协议!" }]}
          >
            <Checkbox>
              我已阅读并同意
              <a href="#" target="_blank" rel="noopener noreferrer">
                服务协议
              </a>{" "}
              和
              <a href="#" target="_blank" rel="noopener noreferrer">
                隐私政策
              </a>
            </Checkbox>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
};

export default Login;
